package com.tong.embeddedvr.interceptor;

import com.tong.embeddedvr.constant.ConstantPool;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;
import java.util.UUID;

/**
 * TODO
 *
 * @author gcl
 * @version 1.0
 * @Description  webSocket专用的拦截器,后期将uuid修改userId
 * @date 2021/3/4 19:46
 */
public class WebSocketInterceptor  implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        if (request instanceof ServletServerHttpRequest) {  //这是为了验证什么?
            //保证每个用户都有一个唯一的uuid属性
            String uuid = UUID.randomUUID().toString();
            uuid.replace("-","");
            attributes.put(ConstantPool.WEBSSH_USER_UUID,uuid);  //注意这个map就叫attribute
            return true;  //通过
        } else {
            return false;  //拒绝
        }
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}
